﻿@model RH.Web.ViewModels.ZGLModel
@using RH.Web.ViewModels;
@using RH.Common.Extensions;
@using RH.Web.Utility;
@using RH.Common.DB;
@{
    List<ZGLModelGroupProperty> _AllProperties = new List<ZGLModelGroupProperty>();
    if(Model != null){
        foreach(var group in Model.Group){
            _AllProperties.AddRange(group.Property);
        }
    }
    RH.Domain.UtilityDAO _Dao = new RH.Domain.UtilityDAO();
}
@functions
{
    public ZGLModelGroupProperty GetPropertyByName
        (String propertyName, List<ZGLModelGroupProperty> allProducts)
    {
        return allProducts.FirstOrDefault(P => String.Compare(P.Name, propertyName, true) == 0);        
    }
}
@if (Model != null)
{   
    if (Model.Filters.Count > 0)
    {
        foreach (var filter in Model.Filters)
        {
            <h4>@filter.Label</h4>
            ///Filter may be has a Referenced Property, And the list items are described in that property
            if (filter.Options.Count == 0)
            {
                var property = GetPropertyByName(filter.SearchField, _AllProperties);
                if (property != null)
                {
                    ///Generate Properties from Static Options Given in ZGL
                    if (property.Option.Count > 0)
                    {                        
                        <ul class="search-filters" searchfield="@filter.SearchField">
                            @for (int i = 0; i < property.Option.Count; i++)
                            {
                                var option = property.Option[i];
                                String elementID = String.Format("{0}_{1}", property.Name, i);
                                <li><input id="@(elementID)" type="checkbox" value="@option.Value" /><label for="@(elementID)">@option.Text</label></li>
                            }
                        </ul>
                    }
                    ///Generate properties from Database
                    else
                    {
                        if (!property.ListProvider.IsNullOrEmpty() && !property.ValueColumn.IsNullOrEmpty() && !property.TextColumn.IsNullOrEmpty())
                        {
                            System.Data.DataTable dt = _Dao.GetLookUpData(property.ListProvider, property.ValueColumn, property.TextColumn);
                            <ul class="search-filters" searchfield="@filter.SearchField">
                            @for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                System.Data.DataRow row = dt.Rows[i];
                                String elementID = String.Format("{0}_{1}", property.Name, i);
                                <li><input id="@elementID" type="checkbox" value="@row[property.ValueColumn]" /><label for="@elementID">@row[property.TextColumn]</label></li>
                            }
                            </ul>
                        }
                    }
                }//Referenced Property in Filters Not Null -> End
            }
            //Filter has Static List Options directly given in ZGL
            else
            {
                String groupName = filter.Label.Replace(' ', '_');
                <ul class="search-filters" searchfield="@filter.SearchField">
                @for (int i = 0; i < filter.Options.Count; i++)
                {
                    var option = filter.Options[i];
                    String elementID = String.Format("{0}_{1}", groupName, i);
                    if(String.Compare(filter.SearchType, "Range", true) == 0){
                        <li><input id="@elementID" type="radio" name="@groupName" value="@(option.Value)" /><label for="@elementID">@option.Text</label></li>
                    }
                    else{
                        <li><input id="@elementID" type="checkbox" value="@(option.Value)" /><label for="@elementID">@option.Text</label></li>
                    }
                }
                </ul>
            }
        }
        <div><a href="javascript:void(0);" onclick="ClearFilters();">Clear Filters</a></div>
    }
}

